﻿// <copyright file="CommentController.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Web.Cms.Admin.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;

    using Blog.Data.Entities;
    using Blog.Services;
    using CleverCMS.Cms.Services;

    [Authorize(Roles = "Admin")]
    public class CommentController : Controller
    {
        private readonly BlogEntryService   m_blogEntryService;
        private readonly PageService        m_pageService;

        public CommentController(BlogEntryService   blogEntryService,
                                 PageService        pageService)
        {
            m_blogEntryService = blogEntryService;
            m_pageService = pageService;
        }

        public ActionResult List()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ListCommentaries(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null) 
        {
            try
            {
                int count = 0;
                var allComments = m_pageService.GetAllComments(jtStartIndex, jtPageSize, jtSorting, out count).ToList();
                return Json(new
                {
                    Result = "OK", 
                    Records = allComments.Select(x => new
                    {
                        Id = x.Id,
                        Comment = x.Comment,
                        Page = x.Page.PageTitle,
                        Name = x.Name,
                        Created = x.CreatedAt.ToString("dd-MM-yyyy hh:mm:ss")
                    }), 
                    TotalRecordCount = count
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        public ActionResult Delete(int? Id)
        {
            if (!ModelState.IsValid)
                return Json(new { Result = "ERROR", Message = "", success = false, error = "Unknown error" });

            try
            {
                if (Id != null) 
                {
                    m_pageService.DeleteComment(Id.Value);
                    return Json(new { Result = "OK", Message = "", success = true, error = "", settingId = Id });
                }
                else
                    return Json(new { Result = "ERROR", Message = "", success = false, error = "Unknown error" });
            }
            catch (Exception exc)
            {
                var v = new { Result = "ERROR", Message = exc.Message, success = false, error = exc.Message };
                return Json(v);
            }
        }

        #region Post Comments
        public ActionResult ListPosts()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ListPostCommentaries(int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = null)
        {
            try
            {
                int count = 0;
                IEnumerable<BlogEntryComment> allComments = m_blogEntryService.GetAllComments(jtStartIndex, jtPageSize, jtSorting, out count).ToList();
                return Json(new
                {
                    Result = "OK",
                    Records = allComments.Select(x => new
                    {
                        Id = x.Id,
                        Comment = x.Comment,
                        Page = x.BlogEntry.Header,
                        Name = x.Name,
                        Created = x.CreatedAt.ToString("dd-MM-yyyy hh:mm:ss")
                    }),
                    TotalRecordCount = count
                });
            }
            catch (Exception ex)
            {
                return Json(new { Result = "ERROR", Message = ex.Message });
            }
        }

        public ActionResult DeletePostComment(int? Id)
        {
            if (!ModelState.IsValid)
                return Json(new { Result = "ERROR", Message = "", success = false, error = "Unknown error" });

            try
            {
                if (Id != null)
                {
                    m_blogEntryService.DeleteComment(Id.Value);
                    return Json(new { Result = "OK", Message = "", success = true, error = "", settingId = Id });
                }
                else
                    return Json(new { Result = "ERROR", Message = "", success = false, error = "Unknown error" });
            }
            catch (Exception exc)
            {
                var v = new { Result = "ERROR", Message = exc.Message, success = false, error = exc.Message };
                return Json(v);
            }
        }
        #endregion

    }
}